/*
 * MapReferencePoint.java
 *
 * Created on 27 avril 2007, 16:24
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package courselog.maputils;

import courselog.gpsutils.*;
// import ewe.ui.MessageBox;
import ewe.util.Copyable;

/**
 *
 * @author rigal
 */
public class MapReferencePoint  extends GeoPoint implements Copyable {

    /** Code to read a MapReferencePoint CSV line.
     */
    public static final String CSVLINECODE="VVVII";
    /**
     * Coordinates in the bitmap of the reference point.
     */
    public int iX;
    public int iY;

    /** Creates a new instance of MapReferencePoint */
    public MapReferencePoint(){
        initialise();
    }

    /**
     *  Initialise the Reference point with (-1,-1) coordinates e.g.
     * invalid ones.
     * @Override GeoPoint.initialise()
     */
    public void initialise() {
        iX=-1;
        iY=-1;
        super.initialise();
    }

    /**
     * Copy of <i>param</i> reference point into <b>this</b>.
     * @param param Object to copy from, if not a MapReferencePoint, no copy.
     * @Override GeoPoint.copyFrom(Object)
     */
    public void copyFrom(Object param) {
        if(!(param instanceof MapReferencePoint)) {
            return;
        }
        super.copyFrom(param);
        MapReferencePoint pt= (MapReferencePoint) param;
        iX= pt.iX;
        iY= pt.iY;
    }

    /**
     * Create a new copy of <b>this</b> reference point.
     * @Override GeoPoint.getCopy()
     */
    public Object getCopy() {
        MapReferencePoint pt= new MapReferencePoint();
        pt.copyFrom(this);
        return pt;
    }

    /**
     * Read a line of CSV values into a MapReferencePoint.
     */
    public boolean readFromCsvLine(String l) {
        double[] vDbl=new double[3];
        int[] vInt=new int[2];
        char[] vCh=new char[1];

        // Parse content
//        new MessageBox("MapReferencePoint.readFromCsvLine",l,MessageBox.OKB).execute();

        Converters.parseCSVSentence(CSVLINECODE, l.getBytes(),0,
                l.length()-1, vDbl, vInt, vCh);
        // Put values in fields
//        new MessageBox("72:MapReferencePoint.readFromCsvLine","("+vDbl[0]+","+vDbl[1]+")",MessageBox.OKB).execute();
        this.lat=vDbl[0]; // Latitude
        this.lon=vDbl[1]; // Longitude
        this.altSeaLevel=vDbl[2]; // Altitude
        this.iX=vInt[0]; // X coordinate in bitmap
        this.iY=vInt[1]; // Y coordinate in bitmap
        return true;
    }

    /**
     * Formats this map reference point in a buffer so that it can be
     * written to the reference file.
     * Values are formatted in order to be readable as csv values.
     * @param buff buffer to write in.
     * @return Length of the formatted values in the buffer.
     * @Override GeoPoint.formatInBuffer(byte[])
     */
    public int formatInBuffer(byte[] buff) {
        int cur=0;
        // Write latitude
        cur=Converters.writeDblInBuffer(buff, cur, 9, 6, this.lat);
        buff[cur]=','; cur++;
        // Write longitude
        cur=Converters.writeDblInBuffer(buff, cur, 10, 6, this.lon);
        buff[cur]=','; cur++;
        // Write altitude
        cur=Converters.writeDblInBuffer(buff, cur, 7, 2, this.altSeaLevel);
        buff[cur]=','; cur++;
        // Write X coordinate in bitmap
        cur=Converters.writeIntInBuffer(buff, cur, 5, iX);
        buff[cur]=','; cur++;
        // Write Y coordinate in bitmap
        cur=Converters.writeIntInBuffer(buff, cur, 5, iY);

        buff[cur]='\n'; cur++;
        return cur;
    }
}
